import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_widgets/config/kq_global.dart';
import 'package:kq_flutter_widgets/resources/images.dart';
import '../../resources/kq_theme_colors.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';

///密码输入框，可以设置[isShowRightIcons]可显示隐藏和显示密码按钮
class KqPasswordView extends StatefulWidget {
  ///没聚焦时输入框边框
  final InputBorder? enabledBorder;

  ///聚焦时输入框边框
  final InputBorder? focusedBorder;

  ///输入长度
  final int? maxLength;

  ///提示文本
  final String? hint;

  ///提示文本样式
  final TextStyle? hintStyle;

  ///输入文本样式
  final TextStyle? inputStyle;

  ///右边图片，必须传两张，第一张为展示密码图片，第二张为隐藏密码图片，只支持本地图片
  final List<String>? rightIcons;

  ///光标颜色
  final Color? cursorColor;

  /// 键盘类型
  final TextInputType keyboardType;

  /// 输入内容限制
  final List<TextInputFormatter>? inputFormatters;

  ///输入回调
  final Function(String value)? onChange;

  ///是否展示右侧显示与隐藏图标
  final bool isShowRightIcons;

  ///焦点监听
  final Function(bool hasFocus)? onFocusChange;

  const KqPasswordView({
    super.key,
    this.keyboardType = TextInputType.text,
    this.maxLength,
    this.hint,
    this.hintStyle,
    this.inputStyle,
    this.rightIcons,
    this.enabledBorder,
    this.focusedBorder,
    this.cursorColor,
    this.inputFormatters,
    this.onChange,
    this.isShowRightIcons = false,
    this.onFocusChange,
  });

  @override
  State<StatefulWidget> createState() => KqPasswordViewState();
}

class KqPasswordViewState extends State<KqPasswordView> {
  bool _isObscure = true;
  final FocusNode _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    _focusNode.addListener(() {
      widget.onFocusChange?.call(_focusNode.hasFocus);
    });
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: Stack(
        alignment: Alignment.center,
        children: [
          TextField(
            keyboardType: widget.keyboardType,
            inputFormatters: widget.inputFormatters,
            maxLines: 1,
            maxLength: widget.maxLength,
            cursorColor: widget.cursorColor ?? KqThemeColors.bgBlue,
            decoration: InputDecoration(
              contentPadding: widget.isShowRightIcons
                  ? EdgeInsets.only(right: 46.r)
                  : EdgeInsets.zero,
              enabledBorder: widget.enabledBorder ??
                  const UnderlineInputBorder().copyWith(
                    borderSide: BorderSide(
                      color: KqThemeColors.bgF0,
                      width: 1.4.r,
                    ),
                  ),
              focusedBorder: widget.focusedBorder ??
                  const UnderlineInputBorder().copyWith(
                    borderSide: BorderSide(
                      color: KqThemeColors.bgBlue,
                      width: 1.4.r,
                    ),
                  ),
              hintText: widget.hint,
              hintStyle: widget.hintStyle,
            ),
            style: widget.inputStyle,
            obscureText: _isObscure,
            onChanged: (value) {
              widget.onChange?.call(value);
            },
            focusNode: _focusNode,
          ),
          Visibility(
            visible: widget.isShowRightIcons,
            child: Positioned(
              right: 0,
              child: KqInkWell(
                onTap: () {
                  _isObscure = !_isObscure;
                  setState(() {});
                },
                enableRipple: false,
                child: Container(
                  width: 46.r,
                  height: 46.r,
                  alignment: Alignment.centerRight,
                  child: KqImage.assets(
                    url: _isObscure
                        ? widget.rightIcons == null ||
                                widget.rightIcons!.length < 2
                            ? Images.commonIcEyeclose
                            : widget.rightIcons![1]
                        : widget.rightIcons == null ||
                                widget.rightIcons!.length < 2
                            ? Images.commonIcEye
                            : widget.rightIcons![0],
                    width: 24.r,
                    height: 24.r,
                    package: widget.rightIcons == null ||
                            widget.rightIcons!.length < 2
                        ? KqGlobal.packageName
                        : null,
                    fit: BoxFit.contain,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _focusNode.dispose();
  }
}
